package com.ls.aicoderbackend.core.saver;

import cn.hutool.core.util.ObjUtil;
import com.ls.aicoderbackend.ai.model.HtmlCodeResult;
import com.ls.aicoderbackend.ai.model.MultiFileCodeResult;
import com.ls.aicoderbackend.exception.BusinessException;
import com.ls.aicoderbackend.exception.ErrorCode;
import com.ls.aicoderbackend.exception.ThrowUtils;
import com.ls.aicoderbackend.model.enums.CodeGenTypeEnum;

import java.io.File;

public class CodeFileSaveExecutor {

    private static final HtmlFileSaveTemplate htmlFileSaveTemplate = new HtmlFileSaveTemplate();
    private static final MultiFileSaveTemplate multiFileSaveTemplate = new MultiFileSaveTemplate();

    public static File saveCodeFile(Object codeResult, CodeGenTypeEnum codeGenTypeEnum, Long appId) {
        ThrowUtils.throwIf(ObjUtil.isNull(codeGenTypeEnum), ErrorCode.PARAMS_ERROR, "代码类型不能为空");
        return switch (codeGenTypeEnum) {
            case HTML -> htmlFileSaveTemplate.saveCode((HtmlCodeResult) codeResult, appId);
            case MULTI_FILE -> multiFileSaveTemplate.saveCode((MultiFileCodeResult) codeResult, appId);
            default -> throw new BusinessException(ErrorCode.PARAMS_ERROR, "不支持的代码类型");
        };
    }
}
